/****************************************************************************
 * 2023.9 DESKTOP-C9GBBP5
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace Survivor
{
	public partial class ExpUpgradePanel : UIElement, IController
	{
		private void Awake()
		{
			InitCoinUpgradeItemPrefab();
		}

		private void InitCoinUpgradeItemPrefab()
		{
			var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();
			foreach (var expUpgradeItem in expUpgradeSystem.ExpUpgradeItemList)
			{
				AbilityItemPrefab.InstantiateWithParent(AbilityRoot)
					.Self(btn =>
					{
						var descTxt = btn.GetComponentInChildren<Text>();
						var itemCache = expUpgradeItem;
						descTxt.text = $"{itemCache.Describe}";
						var btnCache = btn;
						btnCache.onClick.AddListener(() =>
						{
							itemCache.Upgrade();
							Hide();
							Time.timeScale = 1;
							AudioKit.PlaySound("AbilityLevelUp");
						});
						itemCache.Visible.RegisterWithInitValue(visible =>
						{
							btnCache.gameObject.SetActive(visible);
						}).UnRegisterWhenGameObjectDestroyed(btnCache);

						itemCache.CurrentLevel.RegisterWithInitValue(lv =>
						{
							descTxt.text = $"{itemCache.Describe}";
						}).UnRegisterWhenGameObjectDestroyed(btnCache);
					});
			}
		}
		protected override void OnBeforeDestroy()
		{
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}